/**
 * 
 */
package com.alstudio.module.xmpp.protocol.xmpp.parser.iq.loadmore;

import java.util.ArrayList;

import android.text.TextUtils;

import com.alstudio.core.voip.CallDetail;
import com.alstudio.module.xmpp.protocol.xmpp.ALXmppEventListener;
import com.alstudio.module.xmpp.protocol.xmpp.packact.ALIQ;
import com.alstudio.module.xmpp.protocol.xmpp.packact.IQNameSpace;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser;
import com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.IQXmlParseEventCallback;
import com.alstudio.pojo.module.user.User;
import com.alstudio.utils.formater.numeric.ALNumericUtils;
import com.alstudio.utils.log.ALLog;

/**
 * 解析發現、排行榜、好友列表、粉絲列表、邂逅等需要分段加载的协议
 * 
 * @author alonso lee
 * 
 */
public class LoadMoreReusltParser extends ALBaseIQParser implements
		IQXmlParseEventCallback {

	private int code = 0;
	private boolean hasMore = false;
	private ArrayList<User> users;
	private int count;
	// 相互关注
	private String specialFavoritesCount;

	private String ns;
	private String version;

	private ArrayList<CallDetail> calls;
	private String jid;
	private boolean isForCallDetail = false;

	@Override
	public void parseIQPackage(ALIQ packact, String stream,
			ALXmppEventListener listener) throws Exception {
		// TODO Auto-generated method stub
		code = 0;
		count = 0;
		version = "";
		isForCallDetail = false;
		specialFavoritesCount = "";
		ns = packact.getNameSpace();
		jid = "";
		if (IQNameSpace.NS_VOIP_CALL_HISTORY_DETAIL.equals(ns)) {
			isForCallDetail = true;
		}

		hasMore = false;
		mListener = listener;
		users = null;
		users = new ArrayList<User>();

		calls = null;
		calls = new ArrayList<CallDetail>();
		startParse(packact, stream, this);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.ALBaseIQParser#onCallback
	 * ()
	 */
	@Override
	public void onCallback() {
		// TODO Auto-generated method stub
		if (mListener != null) {
			if (IQNameSpace.NS_DISCOVERY.equals(ns)) {
				mListener.onGetDiscoveryUsersResult(code, hasMore, users);
			} else if (IQNameSpace.NS_RANKING_GLAMOUR.equals(ns)) {
				mListener.onGetRankUsersResult(0, code, hasMore, users);
			} else if (IQNameSpace.NS_RANKING_RICH.equals(ns)) {
				mListener.onGetRankUsersResult(1, code, hasMore, users);
			} else if (IQNameSpace.NS_RANKING_SEXY.equals(ns)) {
				mListener.onGetRankUsersResult(2, code, hasMore, users);
			} else if (IQNameSpace.NS_FAVORITES_LIST.equals(ns)) {
				// 好友列表
				mListener.onGetFriendsAndFansResult(0, code, hasMore, users,
						count, version, specialFavoritesCount);
			} else if (IQNameSpace.NS_FANS_LIST.equals(ns)) {
				// 粉丝列表
				ALLog.d("回调粉丝列表");
				mListener.onGetFriendsAndFansResult(1, code, hasMore, users,
						count, version, specialFavoritesCount);
			} else if (IQNameSpace.NS_FAVORITES_EACH_OTHER.equals(ns)) {
				// 相互关注
				ALLog.d("回调相互关注");
				mListener.onGetFavoritesEachOtherResult(code, hasMore, users);
			} else if (IQNameSpace.NS_VOIP_RECORD_WITH_STRANGER.equals(ns)) {
				// 陌生人通话记录
				mListener.onGetVoipRecordResult(code, 0, hasMore, users);
				ALLog.d("陌生人通话记录");

			} else if (IQNameSpace.NS_VOIP_RECORD_WITH_FRIENDS.equals(ns)) {
				// 好友通话记录
				mListener.onGetVoipRecordResult(code, 1, hasMore, users);
				ALLog.d("好友通话记录");

			} else if (IQNameSpace.NS_VOIP_RECORD_WITH_BAOMIHUA.equals(ns)) {
				// 爆米花通话记录
				mListener.onGetVoipRecordResult(code, 2, hasMore, users);
				ALLog.d("爆米花通话记录");
			} else if (IQNameSpace.NS_VOIP_CALL_HISTORY_DETAIL.equals(ns)) {
				// 通话记录详情
				mListener.onGetVoipCallHistoryDetailResult(code, jid, calls,
						hasMore);
				ALLog.d("通话记录详情");
			}

		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processErrorStartTag(java.lang.String)
	 */
	@Override
	public void processErrorStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ERROR_CODE_TAG)) {
			code = ALNumericUtils.parseInt(getAttValue("code"), 0);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processSetStartTag(java.lang.String)
	 */
	@Override
	public void processSetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processGetStartTag(java.lang.String)
	 */
	@Override
	public void processGetStartTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processResultStartTag(java.lang.String)
	 */
	@Override
	public void processResultStartTag(String tag) {
		// TODO Auto-generated method stub
		if (tag.equals(ITEMS_TAG)) {
			String tmp = getAttValue("more");
			if (!TextUtils.isEmpty(tmp) && tmp.equals(TRUE)) {
				hasMore = true;
			}
		} else if (tag.equals(JID_TAG)) {
			jid = readText();
		} else if (tag.equals(ITEM_TAG)) {
			if (isForCallDetail) {
				CallDetail detail = new CallDetail();
				//呼入呼出 
				detail.setCaller(ALNumericUtils.parseInt(
						getAttValue("is_caller"), 0) == 1 ? true : false);
				//通话状态
				detail.setState(ALNumericUtils
						.parseInt(getAttValue("state"), 0));
				//通话生成时间
				detail.setTime(ALNumericUtils.parseLong(getAttValue("time"),
						(System.currentTimeMillis() / 1000)));
				//通话时长
				detail.setCallingTime(ALNumericUtils.parseLong(
						getAttValue("duration"), 0));
				calls.add(detail);
			} else {
				User u = new User();
				// jid
				u.setJid(getAttValue(JID_TAG));
				// 昵称
				u.setNick(getAttValue(NICK_TAG));
				// 性别
				u.setSex(getAttValue(SEX_TAG));
				// 头像
				u.setImageFileId(getAttValue(AVATAR_TAG));
				// 在线时间
				u.setLastLoginTimeStamp(ALNumericUtils.parseLong(
						getAttValue(LAST_ONLINE_TAG), 0));
				// 年龄
				u.setAge(getAttValue(AGE_TAG));
				// 省会
				u.setProvince(getAttValue(PROVICE_TAG));
				// 城市
				u.setCity(getAttValue(CITY_TAG));
				// 魅力
				u.setGlamour(getAttValue(GLAMOUR_TAG));

				// vip级别
				u.setViplevel(ALNumericUtils.parseInt(
						(getAttValue(VIP_LEVEL_TAG)), 0));
				// 金币
				u.setGold(getAttValue(GOLD_TAG));

				// 好友关系
				u.setIsfriend(getAttValue(IS_FRIEND_TAG));

				// // 特殊关系
				// u.setIsfriend(getAttValue(IS_SPECIAL_TAG));
				// V认证状态
				u.setVauthed(ALNumericUtils.parseInt(getAttValue(VAUTHED_TAG),
						0));
				// 签名
				u.setSignature(getAttValue(SIG_TAG));
				// 积分，用于女神榜
				u.setPoints(getAttValue(POINTS_TAG));
				// 级别
				u.setStarState(ALNumericUtils
						.parseInt(getAttValue(STAR_TAG), 0));

				users.add(u);
				ALLog.d("添加" + u.getNick());
			}

		} else if (COUNT_TAG.equals(tag)) {
			count = ALNumericUtils.parseInt(readText(), 0);
			ALLog.d("数量是" + count);
		} else if (VERSION_TAG.equals(tag)) {
			version = readText();
			ALLog.d("版本号" + version);
		} else if ("friend_count".equals(tag)) {
			// 相互关注列表
			specialFavoritesCount = readText();
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processEndTag(java.lang.String)
	 */
	@Override
	public void processEndTag(String tag) {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processStartDocument()
	 */
	@Override
	public void processStartDocument() {
		// TODO Auto-generated method stub

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.alstudio.module.xmpp.protocol.xmpp.parser.iq.IQParseEventHandler.
	 * IQXmlParseEventCallback#processEndDocument()
	 */
	@Override
	public void processEndDocument() {
		// TODO Auto-generated method stub
		onCallback();
	}

}
